﻿using System;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;

namespace Actya.Core.Infrastructure.Logging
{
	/// <summary>
	/// Logging interceptor.
	/// </summary>
	public class LoggingInterceptor : IInterceptor
	{
		public void Intercept(IInvocation invocation)
		{
			var logger = LogManager.GetLogger(invocation.TargetType);
			try
			{
				if (logger.IsDebugEnabled)
				{
					var sb = new StringBuilder(invocation.TargetType.FullName)
						.Append(".")
						.Append(invocation.Method)
						.Append("(")
						.Append(String.Join(", ", invocation.Arguments.Where(o => o != null).Select(o => o.ToString())))
						.Append(")");
					logger.Debug(sb.ToString());
				}
				invocation.Proceed();
			}
			catch (Exception e)
			{
				logger.Error(e);
				throw;
			}
		}
	}
}
